//
//  wholeLabelViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/8.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "wholeLabelViewController.h"
#import "labelRecommendModel.h"
#import "recommendCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "weddingCaseGroupsLabelCell.h"

#define kHeight [UIScreen mainScreen].bounds.size.height
#define kWidth [UIScreen mainScreen].bounds.size.width

@interface wholeLabelViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
// scroll
@property (nonatomic, retain)UIScrollView *backgroundScrollView;
// 集合视图
@property (nonatomic, retain)UICollectionView *labelRecommendCollectionView;
// 全部标签分组的数组
@property (nonatomic, retain)NSMutableArray *wholeLabelGroupsArr;
// 婚礼案例分组标签
@property (nonatomic, retain)UILabel *weddingCaseGroupsLabel;
// 婚礼案例分组标签集合视图
@property (nonatomic, retain)UICollectionView *weddingCaseGroupsLabelCollectionView;
@end

@implementation wholeLabelViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.labelRecommendInfoArr = [NSMutableArray array];
        self.wholeLabelGroupsArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc {
    [_labelRecommendInfoArr release];
    [_backgroundScrollView release];
    [_labelRecommendCollectionView release];
    [_wholeLabelGroupsArr release];
    [_weddingCaseGroupsLabelCollectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"全部标签";
    self.navigationController.navigationBar.translucent = NO;
    //
//    [self createData];
    //
    [self createView];
    
}
//
- (void)createData {
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    // 获取管理类
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    // 用管理类调换用get请求
    [manager GET:@"http://www.hunliji.com/v1/api/app/marks.json?kind=global&cid=0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 解析成功, 接收数据
        NSMutableDictionary *resultDic = responseObject;
        NSMutableArray *groupsArr = resultDic[@"groups"];
        for (NSMutableDictionary *tempDic in groupsArr) {
            labelRecommendModel *model = [[labelRecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.wholeLabelGroupsArr addObject:model];
        }
        
        [hub hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 解析失败,显示错误信息
        
    }];
}
//
- (void)createView {
    // scroll
    self.backgroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight * 2)];
    [self.view addSubview:self.backgroundScrollView];
    [self.backgroundScrollView release];
    // 设置偏移范围
    self.backgroundScrollView.contentOffset = CGPointMake(0, kHeight);
    // 不设置scrollview按页滚动
    self.backgroundScrollView.pagingEnabled = NO;
    // 设置滚动范围
    self.backgroundScrollView.contentSize = CGSizeMake(0, kHeight);

    
    // 标签推荐集合视图布局
    UICollectionViewFlowLayout *floowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 每个item的大小(cell)
    floowLayout.itemSize = CGSizeMake(100 * kWidth / 375, (kWidth - 0 * kWidth / 375) / 3 );
    // 设置最小的行间距
    floowLayout.minimumLineSpacing = 10 * kWidth / 375;
    // 取代上面的行间距设置(section中cell的边界范围)
    floowLayout.sectionInset = UIEdgeInsetsMake(10 * kWidth / 375, 10 * kHeight / 667, 10 * kWidth / 375, 10 * kHeight / 667);
    // 标签推荐集合视图
    self.labelRecommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth - 10 * kWidth / 375, 310 * kHeight / 667) collectionViewLayout:floowLayout];
    // 协议
    self.labelRecommendCollectionView.dataSource = self;
    self.labelRecommendCollectionView.delegate = self;
    // 把集合视图添加到header上
    [self.backgroundScrollView addSubview:self.labelRecommendCollectionView];
    // 内存管理
    [_labelRecommendCollectionView release];
    // 为集合视图设置背景颜色
    self.labelRecommendCollectionView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 注册
    // 第一个参数:需要制定注册类型
    // 第二个参数:重用的标志
    [self.labelRecommendCollectionView registerClass:[recommendCell class] forCellWithReuseIdentifier:@"reuseCel"];
    
    
    // 婚礼案例分组标签
//    self.weddingCaseGroupsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 310 * kHeight / 667, kWidth, 40 * kHeight / 667)];
//    [self.backgroundScrollView addSubview:self.weddingCaseGroupsLabel];
//    [self.weddingCaseGroupsLabel release];
//    self.weddingCaseGroupsLabel.text = @"婚礼案例";
    
}
// 标签推荐集合视图的方法
// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return self.labelRecommendInfoArr.count;
}
// 设置cell(标签推荐)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 获取数据
    labelRecommendModel *model = self.labelRecommendInfoArr[indexPath.row];
    //
    recommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCel" forIndexPath:indexPath];
    cell.myImageView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 把图片赋给cell
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.image_path]];
    // 标签推荐图片标题
    UILabel *recommendTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(cell.myImageView.frame.size.width - 20 * kWidth / 375, 5 * kHeight / 667, 15 * kWidth / 375, cell.myImageView.frame.size.height - 10 * kHeight / 667)];
    recommendTitleLabel.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // 获取图片标题信息
    recommendTitleLabel.text = [model name];
    // 文字自适应
    recommendTitleLabel.numberOfLines = 0;
    recommendTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [recommendTitleLabel sizeThatFits:CGSizeMake(recommendTitleLabel.frame.size.width, MAXFLOAT)];
    recommendTitleLabel.frame = CGRectMake(cell.myImageView.frame.size.width - 20 * kWidth / 375, 10 * kHeight / 667, 15 * kWidth / 375, size.height);
    recommendTitleLabel.frame= CGRectMake(recommendTitleLabel.frame.origin.x, recommendTitleLabel.frame.origin.y, recommendTitleLabel.frame.size.width, recommendTitleLabel.frame.size.height);
    // 设置字体
    recommendTitleLabel.font = [UIFont systemFontOfSize:13];
    
    // 把标签加到图片上
    [cell addSubview:recommendTitleLabel];
    // 内存管理
    [recommendTitleLabel release];
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    labelRecommendModel *model = self.labelRecommendInfoArr[indexPath.row];
    labelRecommendDetailViewController *labelRecommendDVC =[[labelRecommendDetailViewController alloc] init];
    // 传值
    labelRecommendDVC.ID = model.id;
    labelRecommendDVC.index = indexPath.row;
    
    labelRecommendDVC.detaiTitle = model.name;
    
    [self.navigationController pushViewController:labelRecommendDVC animated:YES];
    [labelRecommendDVC release];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
